const {successFormat, errorFormat} = require("./resFormat");
const success = (res, data = null, message = '操作成功', code = 200) => {
    const responseData = successFormat(data, message);
    console.log('Success:', responseData);
    res.status(code).json(responseData);
}

const error = (res, message = '请求失败', code = 500, data = null) => {
    const responseData = errorFormat(message, code, data);
    console.log('Error:', responseData);
    res.status(code).json(responseData);
}

// 封装常见错误类型
const badRequest = (res, message = '参数错误') => {
    return error(res, message, 400);
}

const unauthorized = (res, message = '未认证') => {
    return error(res, message, 401);
}

const forbidden = (res, message = '权限不足') => {
    return error(res, message, 403);
}

const notFound = (res, message = '资源不存在') => {
    return error(res, message, 404);
}

const serverError = (res, message = '服务器内部错误') => {
    return error(res, message, 500);
}
module.exports = {
    success,
    error,
    badRequest,
    unauthorized,
    forbidden,
    notFound,
    serverError,
};